package net.cloudcodex.client.character.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.cdo.CharacterCDO;
import net.cloudcodex.client.character.view.CharactersView;
import net.cloudcodex.client.character.view.CharactersView.Message;
import net.cloudcodex.client.character.view.CharactersViewImpl;
import net.cloudcodex.client.character.view.panel.CharacterPanel;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.campaign.character.CharacterDescriptionDTO;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.campaign.character.CompleteCharacterDescriptionDTO;
import net.cloudcodex.shared.dto.home.CharacterSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CharactersViewImpl.class)
public class CharactersPresenter extends
		BasePresenter<CharactersView, CloudCodexEventBus> {
	private final Message message;
	private List<CharacterSummaryDTO> characterSummaryDTOs;
	
	private List<CharacterPresenter> characterPresenterList = new ArrayList<CharacterPresenter>(); 
	private Map<Long, Integer> characterPresenterIndexed = new HashMap<Long, Integer>();
	protected CharacterPanel selectedCharacterPanel;
	private CampaignRPCAsync campaignService;

	@Inject
	public CharactersPresenter(Message pMessage,
			CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;

	}

	@Override
	public void bind() {
		this.view.getCharactersTab().addSelectionHandler(new SelectionHandler<Integer>() {
			
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				CharacterPresenter selectedPresenter = characterPresenterList
				.get(event.getSelectedItem());
				if (!selectedPresenter.isViewInitialized()){
					selectedPresenter.initView();
				}
			}
		});
	}

	private void display() {

		for (CharacterSummaryDTO characterSummaryDTO : characterSummaryDTOs) {
			CharacterPresenter characterPresenter = eventBus
					.addHandler(CharacterPresenter.class);
			characterPresenter.addCharacterSummary(characterSummaryDTO);
			characterPresenterList.add(characterPresenter);
			characterPresenter.display();
		}

	}

	public void onAddCharacterTab(IsWidget view, String tabName,
			Long characterId) {
		int index = this.view.addCharacterView(view, tabName);
		this.characterPresenterIndexed.put(characterId, index);
	}

	public void onInitCharactersTab(
			List<CharacterSummaryDTO> charactersSummaryDTO) {
		this.characterSummaryDTOs = charactersSummaryDTO;
		display();

		eventBus.addTab(view, this.message.getCharactersTabName());
	}

	public void onSelectCharacterTab(Long characterId, String selectedContent) {
		this.view.selectCharacterTab(characterPresenterIndexed.get(characterId));
		CharacterPresenter selectedPresenter = characterPresenterList
				.get(characterPresenterIndexed.get(characterId));
		selectedPresenter.setSelectedContent(selectedContent);
		eventBus.selectTab(view);
		selectedPresenter.refreshContent();




	}
	
}
